package com.marvels.approve.consum.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.PageInfo;
import com.marvels.approve.common.dto.JsonResult;
import com.marvels.approve.common.dto.StateDetailedDTO;
import com.marvels.approve.consum.ApproveConsumerLoanApplyApi;
import com.marvels.approve.loan.dto.LaAttachmentDto;
import com.marvels.approve.loan.dto.LoanExtDataDTO;
import com.marvels.approve.loan.dto.MclaProtocolTemplateInfoDTO;
import com.marvels.approve.loan.dto.UserChannelInfoParamDTO;
import com.marvels.approve.loan.dto.UserChannelInfoResultDTO;
import com.marvels.approve.manager.dto.ApprovalProcessConditionDTO;
import com.marvels.approve.manager.dto.ApprovalProcessInfoDTO;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.loanapply.api.LoanBasicDataApi;
import com.marvels.loanapply.api.LoanExtDataApi;
import com.marvels.loanapply.api.LoanapplyAttachAchieveApi;
import com.marvels.loanapply.api.LoanapplyCheckBillApi;
import com.marvels.loanapply.loan.service.IStateDetailedService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;


@Component
public class ApproveConsumerLoanApplyImpl implements ApproveConsumerLoanApplyApi{

	@Autowired
	private LoanapplyAttachAchieveApi attachAchieveApi;
	
	@Autowired
	private LoanExtDataApi loanExtDataApi;
	
	@Autowired
	private LoanBasicDataApi loanBasicDataApi;
	
	@Autowired
	public IStateDetailedService stateDetailedService;
	
	@Autowired
	private LoanapplyCheckBillApi checkBillApi;
	
	@Override
	public List<LaAttachmentDto> achieveAttachInfo(LaAttachmentDto laAttachmentDto) {
		try {
			List<com.marvels.loanapply.dto.LaAttachmentDto> apiResult = attachAchieveApi.achieveAttachInfo(laAttachmentDto.getAtmtTypes(), laAttachmentDto.getLoanIds(),laAttachmentDto.getFileIds());
			List<LaAttachmentDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), LaAttachmentDto.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.achieveAttachInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.achieveAttachInfo error", e);
			return null;
		}
	}

	@Override
	public List<LaAttachmentDto> compactAttach(LaAttachmentDto laAttachmentDto) {
		try {
			List<com.marvels.loanapply.dto.LaAttachmentDto> apiResult = attachAchieveApi.compactAttach(laAttachmentDto.getFileIds(), laAttachmentDto.getLoanIds());
			List<LaAttachmentDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), LaAttachmentDto.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.compactAttach direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.compactAttach error", e);
			return null;
		}
	}

	@Override
	public MarvelsResultDTO getRelationList() {
		try {
			List<com.marvels.common.dto.BasicDataDTO> list = loanBasicDataApi.getRelationList();
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.getRelationList direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.getRelationList error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e);
		}
	}

	@Override
	public List<StateDetailedDTO> queryStateDetailed(StateDetailedDTO dto) {
		try {
			com.marvels.loanapply.loan.dto.StateDetailedDTO param = new com.marvels.loanapply.loan.dto.StateDetailedDTO();
			BeanUtil.copyProperties(dto, param);
			List<com.marvels.loanapply.loan.dto.StateDetailedDTO> apiResult = stateDetailedService.findStateDetailed(param);
			List<StateDetailedDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), StateDetailedDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.queryStateDetailed direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.queryStateDetailed error", e);
			return null;
		}
	}

	@Override
	public MclaProtocolTemplateInfoDTO selectMclaProtocolTemplateInfo(String templateNo)
			throws MarvelsBusinessException {
		try {
			com.marvels.loanapply.dto.MclaProtocolTemplateInfoDTO apiResult = attachAchieveApi.selectMclaProtocolTemplateInfo(templateNo);
			MclaProtocolTemplateInfoDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), MclaProtocolTemplateInfoDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.selectMclaProtocolTemplateInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.selectMclaProtocolTemplateInfo error", e);
			return null;
		}
	}

	@Override
	public Map<String,String> queryUserNameBymobiles(String mobiles){
		try {
			Map<String,String> result = checkBillApi.selectUserNameByPhone1(mobiles);
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.queryUserNameBymobiles direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.queryUserNameBymobiles error", e);
			return null;
		}
	}
	
	@Override
	public String selectUserIdByName(String userName){
		try {
			if (StrUtil.isBlank(userName)) {
				return null;
			}
			userName = userName.trim().replaceAll(" ", "");
			if (StrUtil.isBlank(userName)) {
				return null;
			}
			String userIds = checkBillApi.selectUserIdByName(userName);
			if(StrUtil.isBlank(userIds)){
				return null;
			}
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.selectUserIdByName direct call success!");
			return userIds;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.selectUserIdByName error", e);
			return null;
		}
	}
	@Override
	public String selectLoanStateNameById(String stateId) {
		try {
			String result = loanExtDataApi.selectLoanStateNameById(stateId);
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.selectLoanStateNameById direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.selectLoanStateNameById error", e);
			return null;
		}
	}

	@Override
	public LoanExtDataDTO selectLoanExtData(LoanExtDataDTO loanExtDataDTO) {
		try {
			com.marvels.loanapply.dto.LoanExtDataDTO param = new com.marvels.loanapply.dto.LoanExtDataDTO();
			BeanUtil.copyProperties(loanExtDataDTO, param);
			com.marvels.loanapply.dto.LoanExtDataDTO apiResult = loanExtDataApi.selectLoanExtData(param);
			LoanExtDataDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), LoanExtDataDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.selectLoanExtData direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.selectLoanExtData error", e);
			return null;
		}
	}

	@Override
	public List<UserChannelInfoResultDTO> selectUserChannelList(UserChannelInfoParamDTO dto) {
		try {
			com.marvels.loanapply.channel.dto.UserChannelInfoParamDTO param = new com.marvels.loanapply.channel.dto.UserChannelInfoParamDTO();
			BeanUtil.copyProperties(dto, param);
			List<com.marvels.loanapply.channel.dto.UserChannelInfoResultDTO> apiResult = loanExtDataApi.selectUserChannelList(param);
			List<UserChannelInfoResultDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), UserChannelInfoResultDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.selectUserChannelList direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.selectUserChannelList error", e);
			return null;
		}
	}

	@Override
	public PageInfo<ApprovalProcessInfoDTO> loanListQuery(ApprovalProcessConditionDTO dto) {
		try {
			com.marvels.loanapply.dto.ApprovalProcessConditionDTO param = new com.marvels.loanapply.dto.ApprovalProcessConditionDTO();
			BeanUtil.copyProperties(dto, param);
			PageInfo<com.marvels.loanapply.dto.ApprovalProcessInfoDTO> apiResult = loanExtDataApi.loanListQuery(param);
			PageInfo<ApprovalProcessInfoDTO> result = (PageInfo<ApprovalProcessInfoDTO>) 
					JSON.parseObject(JSONObject.toJSONString(apiResult), new TypeReference<PageInfo<ApprovalProcessInfoDTO>>(){});
			MarvelsLogUtil.getInstance().info("ApproveConsumerLoanApplyApi.loanListQuery direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerLoanApplyApi.loanListQuery error", e);
			return null;
		}
	}
}
